import java.util.*;

public class PrintCalendar {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Ievadi gadu: ");
        int year = scanner.nextInt();
        
        System.out.println("Ievadi mēnesi no 1 - 12: ");
        int month = scanner.nextInt();
        
        printMonth(year, month);
    }
    
    public static void printMonth(int year, int month) {
        printMonthTitle(year, month);
        printMonthBody(year, month);
    }
    
    public static void printMonthTitle(int year, int month) {
        System.out.println("             " + getMonthName(month)
                + " " + year);
        System.out.println("----------------------------");
        System.out.println("   P   O   T   C   P   S   S");
    }
    
    public static String getMonthName(int month) {
        String monthName = " ";
        switch(month) {
            case 1: monthName = "Janvāris"; break;
            case 2: monthName = "Februāris"; break;
            case 3: monthName = "Marts"; break;
            case 4: monthName = "Aprīlis"; break;
            case 5: monthName = "Maijs"; break;
            case 6: monthName = "Jūnijs"; break;
            case 7: monthName = "Jūlijs"; break;
            case 8: monthName = "Augusts"; break;
            case 9: monthName = "Septembris"; break;
            case 10: monthName = "Oktobris"; break;
            case 11: monthName = "Novembris"; break;
            case 12: monthName = "Decembris"; break;
        }
        return monthName;
    }
    
    public static void printMonthBody(int year, int month) {
        int startDay = getStartDay(year, month);
        int numOfDaysInMonth = getNumOfDaysInMonth(year, month);
        
        int i;
        for(i = 0; i < startDay; i++)
            System.out.print("    ");
        
        for(i = 1; i <= numOfDaysInMonth; i++) {
            System.out.printf("%4d", i);
            
            if((i + startDay) % 7 == 0)
                System.out.println();
        }
        System.out.println();
    }
    
    public static int getStartDay(int year, int month) {
        final int START_DAY_FOR_JAN_1_1800 = 2;
        int totalNumOfDays = getTotalNumOfDays(year, month);
        
        return (totalNumOfDays + START_DAY_FOR_JAN_1_1800) % 7;
    }
    
    public static int getTotalNumOfDays(int year, int month) {
        int total = 0;
        for(int i = 1800; i < year; i++)
            if(isLeapYear(i)) {
                total += 366;
            }
            else
                total+= 365;
        
        for(int i = 1; i < month; i++)
            total += getNumOfDaysInMonth(year, i);
        
        return total;
    }
    
    public static int getNumOfDaysInMonth(int year, int month) {
        if(month == 1 || month == 3 || month == 5
                || month == 7 || month == 8 || month == 10
                || month == 12)
            return 31;
        else if(month == 4 || month == 6 || month == 9
                || month == 11)
            return 30;
        else if(month == 2)
            return isLeapYear(year) ? 29 : 28;
        
        return 0;
    }
    
    public static boolean isLeapYear(int year) {
        return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
    }
}